package com.deyuanyun.pic.settings.controller.commonauthentication;

import com.deyuanyun.pic.settings.service.base.AttachmentService;
import com.deyuanyun.pic.settings.support.cache.SessionCacheSupport;
import com.deyuanyun.pic.common.util.PassWordUtil;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.settings.controller.SettingBaseController;
import com.deyuanyun.pic.settings.controller.dto.UserSessionVO;
import com.deyuanyun.pic.settings.controller.dto.prvlg.UserVO;
import com.deyuanyun.pic.settings.domain.prvlg.User;
import com.deyuanyun.pic.settings.domain.prvlg.UserExpand;
import com.deyuanyun.pic.settings.service.prvlg.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by xu on 2016/1/6.
 * 用户自身的信息管理
 */
@Controller
@RequestMapping("userSelfInfo")
public class UserSelfManagerController extends SettingBaseController {
    @Autowired
    UserService userService;

    @Autowired
    private AttachmentService attachmentService;

    @RequestMapping("userSelfPage.html")
    public ModelAndView userSelfPage(ModelMap modelMap){
        UserSessionVO sessionVO= SessionCacheSupport.getSessionVO();
        UserVO userVO=sessionVO.getUserDetailInfo();
        modelMap.put("session",sessionVO);
        modelMap.put("userDetail",userVO);
        modelMap.put("photoAtta",attachmentService.queryAttachmentTop1(userVO.getId()));
        return forword("/settings/prvlg/userSelfInfo_new");
    }

    /**修改信息*/
    @RequestMapping("modifyUserInfo.json")
    @ResponseBody
    public void modifyUserInfo(String mark,String phone,String email,String oldPass,String newPass){
        User user= null;
        UserExpand userExpand=null;
        UserSessionVO userSessionVO=SessionCacheSupport.getSessionVO();
        if ("info".equals(mark)){
            user=new User();
            user.setMoblie(phone);
            user.setEmail(email);
            userExpand = userService.getUserExpand(userSessionVO.getId());
            if(userExpand != null) {
                userExpand.setMobile(phone);
                userExpand.setEmail(email);
            }
            userService.updateSelfUserInfo(user,  userExpand);
        }
        if ("pass".equals(mark)){
            Asserts.assertTrue(StringUtils.isNotBlank(newPass),"新密码不能为空!");
            user=new User();
            String oldp= PassWordUtil.encodePassword(userSessionVO.getUsername(),oldPass);
            Asserts.assertTrue(userSessionVO.getPwd().equals(oldp),"原密码不正确!");
            String newp= PassWordUtil.encodePassword(userSessionVO.getUsername(),newPass);

            Asserts.assertTrue(!newp.equals(oldp),"新密码不能与原密码相同");

            user.setPwd(newp);
            userService.updateSelfUserInfo(user,null);
        }
        AjaxSupport.sendSuccessText("success","修改成功!");
        return;
    }


}
